﻿using Common;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class HttpHelper
    {
        public static string CallHtml(string URL,string cookie = "",string Method="POST")
        {
            HttpWebRequest request = HttpWebRequest.Create(URL) as HttpWebRequest;
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0";
            request.Method = Method;
            request.Headers.Add("Cookie", cookie);
            request.Credentials = CredentialCache.DefaultCredentials;
            //转换输入参数的编码类型，获取bytep[]数组 
            WebResponse response;
            response = request.GetResponse();
            string reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
            try
            {
                request.GetResponse().Close();
            }
            catch (WebException ex)
            {
                LogHelper.Error("请求页面的时候出错：" + ex.Message);
                throw ex;
            }
            return reader;
        }
        public static string CallHtmlForm(string URL, string cookie = "")
        {
            HttpWebRequest request = HttpWebRequest.Create(URL) as HttpWebRequest;
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36";
            request.Method = "POST";
            request.Headers.Add("Cookie", cookie);
            request.Host = "cszhun.67430000.com";
            request.Referer = "http://cszhun.67430000.com/ArtificialOp/Receivables/srcacc";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Proxy = null;
            request.AllowAutoRedirect = true;
            request.Headers.Add("Origin", "http://cszhun.67430000.com");
            //转换输入参数的编码类型，获取bytep[]数组 
            WebResponse response;
            ////整数据  
            //string postData = "srcacc=jx100866";
            string postData = "savetype=318&dps_amount=12&dps_bet_amount=4&dps_note=test&account%5B%5D=jx100866";
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] bytepostData = encoding.GetBytes(postData);
            request.ContentLength = bytepostData.Length;

            //发送数据  using结束代码段释放
            using (Stream requestStm = request.GetRequestStream())
            {
                requestStm.Write(bytepostData, 0, bytepostData.Length);
            }

            response = request.GetResponse();
            string reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
            try
            {
                request.GetResponse().Close();
            }
            catch (WebException ex)
            {
                LogHelper.Error("请求页面的时候出错：" + ex.Message);
                throw ex;
            }
            return reader;
        }
    }
}
